package com.mzw.huaxiarealestatebackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzw.huaxiarealestatebackend.common.utils.DateFormat;
import com.mzw.huaxiarealestatebackend.entity.BackPage;
import com.mzw.huaxiarealestatebackend.entity.Client;
import com.mzw.huaxiarealestatebackend.entity.Project;
import com.mzw.huaxiarealestatebackend.dao.ProjectDao;
import com.mzw.huaxiarealestatebackend.service.ClientService;

import com.mzw.huaxiarealestatebackend.service.PanService;
import com.mzw.huaxiarealestatebackend.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Project)表服务实现类
 *
 * @author makejava
 * @since 2023-12-22 00:03:03
 */
@Service("projectService")
public class ProjectServiceImpl implements ProjectService {
    @Resource
    private ProjectDao projectDao;

    @Autowired
    private ClientService clientService;

    @Autowired
    private PanService panService;
    @Override
    public List<Project> queryAll() {
        List<Project> projects = this.projectDao.selectList(null);
        return projects;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param pjtNo 主键
     * @return 实例对象
     */
    @Override
    public Project queryById(String pjtNo) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pjt_no", pjtNo);
        Project project = projectDao.selectOne(queryWrapper);
        return project;
    }

    /**
     * 分页查询
     * @param current    当前页码
     * @param size      分页大小
     * @return
     */
    @Override
    public BackPage<Project> queryByPage(int current, int size) {
        BackPage<Project> backPage= new BackPage<>();
        //创建Page对象，传递两个参数：当前页    每页显示记录数
        Page<Project> page = new Page<>(current, size);
        //调用mp方法实现分页
        projectDao.selectPage(page,null);
        //IPage<User> pageModel = userMapper.selectPage(page,null);
        backPage.setContentList(page.getRecords());
        backPage.setCurrentPage(page.getCurrent());
        backPage.setTotalPage(page.getPages());
        backPage.setTotalNum(page.getTotal());
        return backPage;
    }

    /**
     * 模糊查询
     * @param project
     * @return
     */
    @Override
    public List<Project> queryByCondition(Project project) {
        LambdaQueryWrapper<Project> lqw = new LambdaQueryWrapper<>();

        lqw.and(wrapper ->
                wrapper.eq(project.getCltNo() != null, Project::getCltNo, project.getCltNo())
                        .eq(project.getPjtStatus() != null, Project::getPjtStatus, project.getPjtStatus())
                        .like(project.getPjtName() != null, Project::getPjtName, project.getPjtName())
                        .eq(project.getPjtStop() != null, Project::getPjtStop, project.getPjtStop())
        );

        List<Project> projects1 = this.projectDao.selectList(lqw);
        return projects1;
    }


    /**
     * 新增数据
     *
     * @param project 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Project project) {
        project.setPjtBuildtime(DateFormat.getNowDateFormat());
        //设置项目默认完成状态为0
        project.setPjtStatus("0");
        Client client = clientService.queryById(project.getCltNo());
        //设置项目姓名 格式:项目名称-客户名称-客户单位-创建时间
        String projectName = project.getPjtName() + "-" + client.getCltName() + "-" +client.getCltUnit() + "-" +DateFormat.getNowCode();
        // 重新设置项目名称
        project.setPjtName(projectName);
        //设置默认流程为1
        project.setPjtStop("1");
        int insert = this.projectDao.insert(project);
        //创建项目文件夹
        panService.createDirectoryStructure(project.getPjtNo());
        return insert > 0;
    }

    /**
     * 修改数据
     *
     * @param project 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Project project) {
        int update = this.projectDao.update(project);
        return update > 0;
    }

    /**
     * 通过主键删除数据(逻辑删除)
     *
     * @param pjtNo 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String pjtNo) {
        Project project = this.queryById(pjtNo);
        //将状态位改为0
        project.setPjtStop("0");
        boolean update = this.update(project);
        return update;
    }

    /**
     * 删除项目
     * @param pjtNo
     * @return
     */
    @Override
    public boolean delete(String pjtNo) {
        int i = this.projectDao.deleteById(pjtNo);
        return i > 0;
    }


}
